/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.security.customer;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.elsfs.cloud.common.security.filter.phone.sms.PhoneSmsAuthenticationFilter;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.stereotype.Component;

/** 手机号验证码登录接口 */
@Slf4j
@Component
public class PhoneSmsApiPlugin implements GlobalOpenApiCustomizer {

  /**
   * 构建自定义接口
   *
   * @param openApi OpenAPI
   */
  @Override
  public void customise(OpenAPI openApi) {
    LOGGER.info("customer.");
    // 基础信息 构建Operation
    Operation operation = new Operation();
    operation.operationId("PhoneSmsApi");
    operation.summary("手机号验证码登录接口");
    operation.description("根据手机号验证码获取token");
    operation.tags(Collections.singletonList("登录"));
    // 构建参数
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(
        new Parameter()
            .name(PhoneSmsAuthenticationFilter.SPRING_SECURITY_FORM_PHONE_KEY)
            .example("")
            .description("手机号")
            .required(true)
            .schema(new StringSchema())
            .in("query"));
    parameters.add(
        new Parameter()
            .name(PhoneSmsAuthenticationFilter.SPRING_SECURITY_FORM_SMS_KEY)
            .example("")
            .description("验证码")
            .required(true)
            .schema(new StringSchema())
            .in("query"));
    operation.parameters(parameters);
    // 构建响应body
    ApiResponses apiResponses = new ApiResponses();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse
        .description("ok")
        .content(new Content().addMediaType("*/*", new MediaType().schema(new StringSchema())));
    apiResponses.addApiResponse("200", apiResponse);
    operation.responses(apiResponses);
    // 因为要新增自定义的接口，直接这里add
    PathItem pathItem = new PathItem();
    // 该自定义接口为post
    pathItem.post(operation);
    openApi.path(PhoneSmsAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI, pathItem);
  }
}
